<script>
	import Api from '@/util/api.js'
	export default {
		onLaunch: function() {
			console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
			console.log('App Launch',window.location)
			if(window.location.hash == '#/pages/Alipay/Alipay'){
				return 
			}
			var that = this;
			let code = that.getQueryString("code")
			let type_id = 1
			console.log('code',code)
			if(code != ''){
			localStorage.setItem('code',code)
			}
			let id = that.getQueryString("type_id")
			console.log('id',id)
			// 维护员类型id
			if(id != ''){
				// 这是维护员端
			// localStorage.setItem('type',id)
			if(localStorage.getItem("type") == null){
				localStorage.setItem('type',id)
				localStorage.removeItem("userInfo")
				let data = {
					type_id : localStorage.getItem('type')
				}
				that.gotoWXLogin(data)
				return;
			}else{
				if(localStorage.getItem("type") == id){
					console.log('该用户是维护员')
					var LoginUser = JSON.parse(localStorage.getItem('LoginUser')); 
					console.log('LoginUser',LoginUser)
					if(LoginUser !=null){
						var params = {
							id:LoginUser.id
						}
						Api.getDetail(params).then(res=>{
							// console.log('获取用户信息成功',res)
							localStorage.setItem('userInfo',JSON.stringify(res.data))
						})
					}
					
				}else{
					console.log('该用户是从租户端过来的')
					localStorage.setItem('type',id)
					localStorage.removeItem("userInfo")
					let data = {
						type_id : localStorage.getItem('type')
					}
					that.gotoWXLogin(data)
					return;
				}
			}
			}else{
				// 这是租户端
			if(localStorage.getItem("type") == null){
				localStorage.setItem('type',type_id)
				localStorage.removeItem("userInfo")
				let data = {
					type_id : localStorage.getItem('type')
				}
				that.gotoWXLogin(data)
				return;
			}else{
				if(localStorage.getItem("type") == type_id){
					console.log('该用户是租户')
				}else{
					console.log('该用户是从维护员过来的')
					localStorage.setItem('type',type_id)
					localStorage.removeItem("userInfo")
					let data = {
						type_id : localStorage.getItem('type')
					}
					that.gotoWXLogin(data)
					return;
				}
			}
			
			}
			// 获取用户的code
			if(!window.localStorage.getItem('code')||window.localStorage.getItem('code') == undefined ||window.localStorage.getItem('code') == ''||window.localStorage.getItem('code') == null){
				let data = {
					type_id : localStorage.getItem('type')
				}
				that.gotoWXLogin(data)
				return;
			}
			if(!localStorage.getItem('userInfo')){
				that.getUserInfo(localStorage.getItem('code'),localStorage.getItem('type'));
			}
			
			if(localStorage.getItem('LoginUser')){
				var user = JSON.parse(localStorage.getItem('LoginUser'))
				that.getSubscribe(user.id);
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
		gotoWXLogin(data){
			Api.login(data).then(res=>{
				window.location.href = res.data.url
			})
		},
		// 截取url中的code方法
			getQueryString(name) {
		        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		        var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
		        var context = "";
		        if (r != null) context = r[2];
		        reg = null;
		        r = null;
		        return context == null || context == "" || context == "undefined"
		          ? ""
		          : context;
		      },
			  // 登录
			  getUserInfo(code,type_id){
				var that = this
			  	var data = {
			  		code:code,
					state:type_id
			  	}
			  	Api.callback(data).then(res=>{
			  		// window.location.href = res.url
			  		// console.log('获取成功',res)
					var params = {
						id:res.data.id
					}
					localStorage.setItem('LoginUser',JSON.stringify(res.data))
					
				Api.getDetail(params).then(res=>{
					// console.log('获取用户信息成功',res)
					localStorage.setItem('userInfo',JSON.stringify(res.data))
				})
				that.getSubscribe(res.data.id);
			  	}).catch(err=>{
					console.log('捕获异常',err)
					let data = {
						type_id : localStorage.getItem('type')
					}
					that.gotoWXLogin(data)
				})
			  	
			  },
			  // 查询用户是否关注公众号
			  getSubscribe(id){
			  	var that = this
			  		var data = {
			  			id : id
			  		}
			  	 	Api.getSubscribe(data).then(res=>{
			  			console.log('查询是否关注',res)
			  			if(res.data.subscribe == 1){
			  				return true
			  			}else{
			  				uni.reLaunch({
			  					url:'../../pages/index/subscribe'
			  				})
			  			}
			  			
			  		})
			  	
			  }
		},
		
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>
